package com.czl.module_service.fragment.assetUse

import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.event.LiveBusCenter
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.asset.PurchaseOrderAdapter
import com.czl.module_service.databinding.FragmentPurchaseOrderChooseBinding
import com.czl.module_service.extension.loadCompletedData
import com.czl.module_service.viewmodel.assetUse.PurchaseOrderChooseViewModel

@Route(path = AppConstants.Router.Service.F_SERVICE_PURCHASE_ORDER_CHOOSE)
class PurchaseOrderChooseFragment :
    BaseFragment<FragmentPurchaseOrderChooseBinding, PurchaseOrderChooseViewModel>() {

    private val mAdapter: PurchaseOrderAdapter by lazy {
        PurchaseOrderAdapter(this).apply {

        }
    }

    override fun initContentView(): Int {
        return R.layout.fragment_purchase_order_choose
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()
        viewModel.tvTitle.set("采购单管理")
        initAdapter()
        reload()
    }


    private fun initAdapter() {
        binding.recyclerView.run {
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            this.loadCompletedData(
                data?.hasNextPage,
                viewModel.currentPage == 2,
                data != null,
                data?.total,
                data?.list,
                mAdapter,
                binding.recyclerView,
                binding.smartCommon
            )
        })
        viewModel.uc.loadSubmitEvent.observe(this, { data ->
            LiveBusCenter.postAddAssetPurchaseSortEvent(data)
            back()
        })
    }

    override fun reload() {
        super.reload()
        binding.smartCommon.autoRefresh()
    }

}